<template>
	<view>
		<view class="nav">
			<view :class="navid == index?'navitem active':'navitem'" v-for="(item,index) in navlist" :key="index"
				@click="changeNav(index)">
				{{item}}
			</view>
		</view>
		<!-- 购买商品列表 -->
		<view class="buy-list">
			<view class="row">
				<view class="goods-info" v-for="(item,index) in buylist" :key="index">
					<view class="img">
						<image :src="item.cover_path"></image>
					</view>
					<view class="info">
						<view class="title">{{item.name}}</view>
						<view class="spec">单价 : {{item.price}}</view>
						<view class="price-number">
							<view class="price">￥{{item.amount}}</view>
							<view class="number">
								<view class="sub" @tap.stop="sub(item)">
									<view class="icon jian"></view>
								</view>
								<view class="input">
									<input type="number" :value="item.num" disabled="true" />
								</view>
								<view class="add" @tap.stop="add(item)">
									<view class="icon jia"></view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>

		<!-- 明细 -->
		<view class="detail">
			<view class="row">
				<view class="nominal">
					商品金额
				</view>
				<view class="content">
					￥{{goodsPrice|toFixed}}
				</view>
			</view>
			<view class="row" v-if="show_point">
				<view class="nominal">
					指定工作室
				</view>
				<view class="content">
					<radio-group @change="changeRadio">
						<label>
							<radio value="1" :checked="is_choose==1" /><text>是</text>
						</label>
						<label>
							<radio value="0" :checked="is_choose==0" /><text>否</text>
						</label>
					</radio-group>
				</view>
			</view>
			<radio-group @change="changePay" class="row">
				<label class="paylabel" v-if="allow_pay" v-for="(item,index) in payList" :key="index">
					<view class="nominal">
						{{item.text}}
					</view>
					<view class="content">
						<radio :value="String(item.value)" :checked="pay_type == item.vakye" />
					</view>
				</label>
			</radio-group>
			<view class="row" v-if="is_choose==1">
				<view class="nominal">
					备注
				</view>
				<view class="content">
					<input type="text" v-model="point_remarks" placeholder="请输入需要指定工作室名称">
				</view>
			</view>
		</view>
		<view class="addbox">
			<view class="aitem" @click="addRole">
				多角色下单
			</view>
			<view class="aitem" @click="toPage('/pages/address/address?type=select')">
				添加保存角色
			</view>
		</view>
		<view class="rolelist">
			<view class="ritem" v-for="(item,index) in roleList" :key="index">
				<view class="del" v-if="index!=0">
					<text @click="del(index)">×</text>
				</view>
				<view class="role-li">
					<view class="left">
						角色名
					</view>
					<view class="right">
						<input type="text" placeholder="请输入角色名" v-model="item.role_name" />
					</view>
				</view>
				<view class="role-li">
					<view class="left">
						子账号
					</view>
					<view class="right">
						<input type="text" placeholder="请输入子账号" v-model="item.son_account" />
					</view>
				</view>
				<view class="role-li">
					<view class="left">
						大区
					</view>
					<view class="right">
						<input type="text" placeholder="请输入区" v-model="item.zone" />
					</view>
				</view>
				<view class="role-li">
					<view class="left">
						服务器
					</view>
					<view class="right">
						<input type="text" placeholder="请输入服" v-model="item.server" />
					</view>
				</view>
				<view class="role-li">
					<view class="left">
						拾取天赋
					</view>
					<view class="right">
						<input type="text" placeholder="请输入拾取天赋" v-model="item.talent" />
						<!-- <uni-data-select v-model="item.talent" :localdata="talentRange" @change="change"
							:clear="false"></uni-data-select> -->
					</view>
				</view>
				<view class="role-li">
					<view class="left">
						目前真实装等
					</view>
					<view class="right">
						<input type="text" placeholder="请输入目前真实装等" v-model="item.eq_level" />
						<!-- <uni-data-select v-model="item.talent" :localdata="talentRange" @change="change"
							:clear="false"></uni-data-select> -->
					</view>
				</view>
				<view class="role-li">
					<view class="left">
						职业
					</view>
					<view class="right">
						<uni-data-select v-model="item.job" :localdata="careerRange" :clear="false"></uni-data-select>
					</view>
				</view>
				<view class="role-li">
					<view class="left">
						阵营
					</view>
					<view class="right">
						<uni-data-select :border="0" v-model="item.camp" :localdata="campRange"
							:clear="false"></uni-data-select>
					</view>
				</view>
				<view class="role-li">
					<textarea maxlength="-1" placeholder="请输入游戏账号信息+手机号" v-model="item.remarks"></textarea>
				</view>
			</view>
		</view>
		<view class="footer">
			<view class="settlement">
				<view class="sum">应付:<view class="money">￥{{goodsPrice|toFixed}}</view>
				</view>
				<button class="btn" @tap="toPay" :loading="loading" :disabled="loading">提交订单</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		OrderFound,
		CreateOrder,
		commonParam
	} from "@/api/api.js"
	export default {
		data() {
			return {
				navlist: ['下单'],
				navid: 0,
				info: [],
				buylist: [], //订单列表
				goodsAmount: 0.0, //商品合计价格
				// sumPrice: 0.0, //用户付款价格
				// freight: 0.00, //运费
				note: '', //备注
				int: 1200, //抵扣积分
				deduction: 0, //抵扣价格
				num: 1,
				recinfo: [],
				vip_discount: 0,
				name: '',
				phone: '',
				goods_id: '',
				cart_ids: '',
				postage: '', //运费
				payTypeList: [],
				code: '',
				show_code: 1,
				is_choose: 0,
				roleList: [{
					zone: '',
					server: '',
					talent: '', //天赋
					job: '', //职业
					camp: '', //阵营
					remarks: '',
					son_account: '',
					eq_level: '',
					role_name: ''
				}],
				talentRange: [],
				careerRange: [],
				campRange: [],
				point_remarks: '',
				strict: true,
				pay_type: '',
				allow_pay: false,
				payList: [],
				loading: false,
				show_point: false,
				template_ids: []
			};
		},
		onLoad(e) {
			this.commonParam()
			if (e.goods_id) {
				this.goods_id = e.goods_id
			}
			if (e.cart_ids) {
				this.cart_ids = e.cart_ids
			}
			this.OrderFound()
		},
		computed: {
			goodsPrice() {
				let sum = 0;
				this.buylist.forEach(item => {
					sum += Number(item.num) * Number(item.price)
				})
				return sum
			}
		},
		onShow() {
			//页面显示时，加载订单信息
		},
		onBackPress() {
			//页面后退时候，清除订单信息
		},
		filters: {
			toFixed: function(x) {
				return parseFloat(x).toFixed(2);
			}
		},
		methods: {
			commonParam() {
				commonParam().then(res => {
					let data = res.data
					this.talentRange = data.talent.map(item => {
						return {
							value: item.id,
							text: item.name
						}
					})
					this.careerRange = data.job.map(item => {
						return {
							value: item.id,
							text: item.name
						}
					})
					this.campRange = data.camp.map(item => {
						return {
							value: item.id,
							text: item.name
						}
					})
				})
			},
			del(i) {
				this.roleList.splice(i, 1)
			},
			addRole() {
				let len = this.buylist.reduce(function(total, titem) {
					return total + parseInt(titem.num)
				}, 0)
				if (this.strict && this.roleList.length >= len) {
					return uni.$showMsg('游戏角色数量超过商品数量')
				}
				this.roleList.push({
					// 	name: '',
					zone: '',
					server: '',
					talent: '', //天赋
					job: '', //职业
					camp: '', //阵营
					remarks: '',
					son_account: '', //子账号
					eq_level: '',
					role_name: ''
				})
			},
			changeRadio(e) {
				this.is_choose = e.detail.value
			},
			changePay(e) {
				this.pay_type = e.detail.value
			},
			changeCheck(e) {
				this.pay_type = e.detail.value
			},
			OrderFound() {
				OrderFound({
					goods_id: this.goods_id
				}).then(res => {
					if (res.code == 0) {
						this.buylist = res.data.goodsData
						this.info = res.data.info
						this.strict = res.data.strict
						this.payList = res.data.payType
						this.allow_pay = res.data.allow_pay
						this.show_point = res.data.showPoint
						this.template_ids = res.data.template_ids
					}
				})
			},

			add(item) {
				item.num++;
			},
			sub(item) {
				if (item.num <= 1) {
					return;
				}
				item.num--;
			},
			sum() {
				this.goodsPrice = this.num * Number(this.buylist.price)
			},
			changeNav(e) {
				this.navid = e
			},
			toPay() {
				var that = this
				that.createorder()
				//本地模拟订单提交UI效果

			},
			createorder() {
				var that = this
				uni.showLoading({
					title: '正在提交订单...'
				})
				that.loading = true
				CreateOrder({
					address_id: that.recinfo.id,
					num: that.num,
					goodsData: that.buylist,
					is_point: that.is_choose,
					point_remarks: that.point_remarks,
					order_games: that.roleList,
					pay_type: that.pay_type
				}).then(res => {
					setTimeout(() => {
						uni.hideLoading();
						if (res.code == 0) {
							let data = res.data
							let paymentData = data.unifiedorder
							if (!data.finishOrder && paymentData) {
								uni.requestPayment({
									timeStamp: paymentData.timeStamp,
									nonceStr: paymentData.nonceStr,
									package: `prepay_id=${paymentData.prepay_id}`,
									signType: 'MD5',
									paySign: paymentData.paySign,
									success: function(result) {
										that.SubscribeMessage()
									},
									fail: function(err) {
										uni.redirectTo({
											url: '/pages/order/index'
										})
									}
								})
							} else {
								uni.showModal({
									title: '订阅消息',
									content: "是否订阅接单提醒和代练确认提醒消息",
									success(data) {
										if (data.confirm) {
											that.SubscribeMessage()
										}
										if (data.cancel) {
											uni.redirectTo({
												url: '/pages/order/index'
											})
										}

									}
								})
							}

						} else {
							that.loading = false
							uni.$showMsg(res.msg)
						}
					}, 500)
				})
			},
			SubscribeMessage() {
				let that = this
				uni.requestSubscribeMessage({
					tmplIds: that.template_ids,
					success(res) {
						uni.redirectTo({
							url: '/pages/order/index'
						})
					},
					fail(err) {
						uni.$showMsg(err.errMsg)
						setTimeout(() => {
							uni.redirectTo({
								url: '/pages/order/index'
							})
						}, 1000)
					}
				})
			},
			//选择收货地址
			selectAddress() {
				uni.navigateTo({
					url: '/pages/address/address?type=select'
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #f3f3f3;
		padding-top: 10px;
		padding-bottom: 50px;
	}

	.rolelist {
		width: 96%;
		margin: 0 auto;
		box-sizing: border-box;
		margin-top: 10px;

		.ritem {
			background: #fff;
			border-radius: 8px;
			padding: 5px 0 10px 0;
			margin-bottom: 10px;

			.del {
				text-align: right;
				padding: 0 10px;
				color: #f00;
				font-size: 22px;
			}
		}

		.role-li {
			display: flex;
			align-items: flex-start;
			justify-content: space-between;
			padding: 10px;
			border-bottom: 1px solid #eee;

			input {
				font-size: 14px;
				height: 30px;
			}

			.right {
				width: 150px;
				text-align: right;

				uni-select {
					text-align: right;
				}
			}

			textarea {
				font-size: 14px;
				width: 100%;
			}

			&:last-child {
				border-bottom: 0;
			}
		}
	}

	.addbox {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 2%;
		position: sticky;
		background: #fff;
		z-index: 99;
		top: 0;
		/* #ifdef H5 */
		top: 44px;

		/* #endif */
		width: 96%;
		margin: 0 auto;
		box-sizing: border-box;
		border-radius: 8px;

		.aitem {
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: center;
			height: 50px;
			font-weight: bold;
			color: #050B18;
			background: #fff;
			border-radius: 5px;

			// &:nth-child(2) {
			// 	background: #050B18;
			// 	color: #fff;
			// }
		}

	}

	.timebox {
		display: flex;
		flex-wrap: wrap;
		width: 96%;
		margin: 0 auto;
		background: #fff;
		border-bottom-left-radius: 10px;
		border-bottom-right-radius: 10px;
		padding: 10px;
		padding-top: 0;
		box-sizing: border-box;

		.t-item {
			font-size: 12px;
			display: flex;
			flex-direction: column;
			align-items: center;
			width: 23%;
			margin: 5px 1%;
			justify-content: center;
			background-color: #eee;
			color: #000;
			height: 50px;
		}

		.active {
			background: #050B18;
			color: #fff;
		}
	}

	radio-group {
		display: block;
		width: 140px !important;
		display: flex;
		align-items: center;
		justify-content: flex-end;

		label {
			display: flex;
			align-items: center;
		}

		.paylabel {
			width: 100% !important;
			justify-content: space-between;
		}

		&.row {
			width: 100% !important;
		}
	}

	.gdline {
		height: 3px;
		width: 50px;
		background-color: #f3f3f3;
		position: absolute;
		bottom: 8px;
		left: 50%;
		transform: translate(-50%);
		overflow: hidden;
	}

	.activeline {
		background-color: #050B18;
		height: 100%;
		transition: ease .3s all;
	}



	.number {
		display: flex;
		justify-content: center;
		align-items: flex-end;

		.input {
			width: 100%;
			height: 45upx;
			margin: 0 10upx;

			input {
				width: 60upx;
				height: 45upx;
				display: flex;
				justify-content: center;
				align-items: center;
				text-align: center;
				font-size: 26upx;
			}
		}


		.sub,
		.add {
			width: 40upx;
			height: 40upx;
			border-radius: 5px;
			border: 1px solid #050B18;
			display: flex;
			justify-content: center;
			align-items: center;

			.icon {
				font-size: 20upx;
				width: 40upx;
				height: 40upx;
				display: flex;
				justify-content: center;
				align-items: center;
				color: #050B18;
			}
		}

		.add {
			background: #050B18;
			color: #fff;

			.icon {
				color: #fff;
			}
		}
	}

	.self {
		width: 96%;
		margin: 0 auto;
		background: #fff;
		border-radius: 5px;
		padding: 10px;
		box-sizing: border-box;
		margin-top: 10px;

		.inputbox {
			display: flex;
			align-items: center;
			font-size: 14px;
			padding: 5px 0;

			text {
				width: 60px;
			}

			input {
				font-size: 14px;
				flex: 1;
				padding: 5px;
				background: #eee;
				border-radius: 4px;
			}
		}

		.selftips {
			display: flex;
			flex-direction: column;
			font-size: 12px;
			color: #999;
			margin-top: 5px;

			.tipsmsg {
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-top: 10px;

				.tipstext {
					display: block;
					width: 90%;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
				}
			}
		}
	}

	.nav {
		display: flex;
		align-items: center;
		border-bottom: 2px solid #050B18;
		width: 96%;
		margin: 0 auto;
		background: #fff;
		border-top-left-radius: 5px;
		border-top-right-radius: 5px;

		.navitem {
			width: 28%;
			text-align: center;
			font-size: 14px;
			height: 40px;
			line-height: 40px;
		}

		.active {
			background: #050B18;
			color: #fff;
			border-top-left-radius: 5px;
			border-top-right-radius: 5px;
		}
	}

	.addr {
		width: 90%;
		padding: 20upx 3%;
		margin: 20upx auto 0 auto;
		// box-shadow: 0upx 5upx 20upx rgba(0, 0, 0, 0.1);
		border-radius: 20upx;
		display: flex;
		background: #fff;

		.icon {
			width: 80upx;
			height: 80upx;
			display: flex;
			align-items: center;

			image {
				width: 60upx;
				height: 60upx;
			}
		}

		.tel-name {
			width: 100%;
			display: flex;
			font-size: 32upx;

			.tel {
				margin-left: 40upx;
			}
		}

		.addres {
			width: 100%;
			font-size: 26upx;
			color: #999;
		}
	}

	.buy-list {
		width: 90%;
		padding: 10upx 3%;
		margin: 20upx auto 20upx auto;
		// box-shadow: 0upx 5upx 20upx rgba(0, 0, 0, 0.1);
		border-radius: 20upx;
		background: #fff;

		.row {
			margin: 30upx 0;

			.goods-info {
				width: 100%;
				display: flex;

				.img {
					width: 22vw;
					height: 22vw;
					border-radius: 10upx;
					overflow: hidden;
					flex-shrink: 0;
					margin-right: 10upx;

					image {
						width: 22vw;
						height: 22vw;
					}
				}

				.info {
					width: 100%;
					height: 22vw;
					overflow: hidden;
					display: flex;
					flex-wrap: wrap;
					position: relative;

					.title {
						width: 100%;
						font-size: 28upx;
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 2;
						// text-align: justify;
						overflow: hidden;
					}

					.spec {
						font-size: 22upx;
						background-color: #f3f3f3;
						color: #a7a7a7;
						height: 40upx;
						display: flex;
						align-items: center;
						padding: 0 10upx;
						border-radius: 20upx;
						margin-bottom: 20vw;
					}

					.price-number {
						position: absolute;
						width: 100%;
						bottom: 0upx;
						display: flex;
						justify-content: space-between;
						align-items: flex-end;
						font-size: 28upx;
						height: 40upx;
						padding-top: 5px;
						box-sizing: border-box;

						.price {
							color: #050B18;
						}

						.number {
							display: flex;
							justify-content: center;
							align-items: center;

						}
					}
				}
			}
		}
	}

	.order {
		width: 90%;
		padding: 10upx 3%;
		margin: 20upx auto 20upx auto;
		// box-shadow: 0upx 5upx 20upx rgba(0, 0, 0, 0.1);
		border-radius: 20upx;
		background: #fff;

		.row {
			margin: 20upx 0;
			height: 40upx;
			display: flex;

			.left {
				font-size: 28upx;
			}

			.right {
				margin-left: 40upx;
				font-size: 26upx;
				color: #999;
				flex: 1;

				input {
					font-size: 26upx;
					color: #999;
				}
			}
		}
	}

	.blck {
		width: 100%;
		height: 100upx;
	}

	.footer {
		width: 100%;
		padding: 0 2%;
		background-color: #fbfbfb;
		height: 100upx;
		display: flex;
		justify-content: flex-end;
		align-items: center;
		font-size: 28upx;
		position: fixed;
		bottom: 0upx;
		z-index: 5;
		background: #fff;
		max-width: 500px;
		box-sizing: border-box;

		.settlement {
			width: 80%;
			display: flex;
			justify-content: flex-end;
			align-items: center;

			.sum {
				width: 50%;
				font-size: 28upx;
				margin-right: 10upx;
				display: flex;
				justify-content: flex-end;

				.money {
					font-weight: 600;
				}
			}

			.btn {
				padding: 0 30upx;
				height: 60upx;
				background-color: #050B18;
				color: #fff;
				display: flex;
				justify-content: center;
				align-items: center;
				font-size: 30upx;
				border-radius: 40upx;
			}
		}
	}

	.detail {
		width: 90%;
		padding: 10upx 3%;
		margin: 20upx auto 20upx auto;
		// box-shadow: 0upx 5upx 20upx rgba(0, 0, 0, 0.1);
		border-radius: 20upx;
		background: #fff;

		.title {
			font-weight: bold;
			padding: 10px 0;
			font-size: 17px;
		}

		.radiobox {
			checkbox-group {
				width: 100%;
			}

			label {
				width: 100% !important;
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin: 15px 0;
			}
		}

		.row {
			height: 60upx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 5px 0;

			.nominal {
				font-size: 28upx;
			}

			.content {
				font-size: 26upx;
				color: #050B18;
				display: flex;
				align-items: center;
			}

			input {
				font-size: 14px;
				text-align: right;
			}
		}

		.note {
			input {
				font-size: 14px;
				color: #999;
				text-align: right;
			}
		}
	}
</style>